require 'View.Base.BehaviourView'
UIFollow3DGo = class("UIFollow3DGo", BehaviourView)

function UIFollow3DGo:Awake(obj)
    BehaviourView.Awake(self, obj)

    self.rtf = self.transform:GetComponent('RectTransform')
    self.rootCanvas = VIEWM.UIRoot:GetComponent('Canvas')
    self.rootCanvasRtf = VIEWM.UIRoot:GetComponent('RectTransform')
end

function UIFollow3DGo:LateUpdate()
    if self.camera == nil then
        self.camera = UnityEngine.Camera.main
    end
    if self.targetGo==nil or utils.IsAvailableGameObject(self.targetGo)==false or self.camera==nil then return end
    local screenPos = self.camera:WorldToScreenPoint(self.targetTf.position+self.targetTfOffset)
    local success, pos = UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(self.rootCanvasRtf, screenPos, self.rootCanvas.worldCamera, nil)
    self.rtf.localPosition = pos + self.offset
end

function UIFollow3DGo:SetTarget(targetTf, offset, targetTfOffset,camera)
    self.targetTf = targetTf
    self.targetGo = self.targetTf.gameObject
    self.offset = offset or Vector3.zero
    self.targetTfOffset = targetTfOffset or Vector3.zero
    self.camera = camera
end